<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('每日跟进')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-approveLog-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">项目名称：</label>
                <div class="col-sm-8">
                    <input name="projectName" class="form-control" type="text" th:field="${project.projectName}" readonly>
                    <input type="hidden" name="projectId" th:field="${project.projectId}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">现场图片：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="multipleFile" name="files" accept="image/png, image/jpg, image/jpeg" type="file" multiple>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">今日进度：</label>
                <div class="col-sm-8">
                    <textarea rows="3" cols="2000"  class="form-control" name="approveComment" placeholder="请输入" required></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script th:inline="javascript">
        var prefix = ctx + "manage/approveLog"
        $("#form-approveLog-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var data = $('#form-approveLog-add').serializeArray();
                data.push({'name': 'entityId', 'value': $("#projectId").val()});
                for (var i = 0; i < fileList.length; i++) {
                    log.info(fileList[i]);
                    data.push({'name': 'fileList['+i+'].fileName', 'value':fileList[i].fileName});
                    data.push({'name': 'fileList['+i+'].oriFileName', 'value':fileList[i].oriFileName});
                    data.push({'name': 'fileList['+i+'].filePath', 'value':fileList[i].filePath});
                }
                log.info(data);
                $.operate.save(prefix + "/add", data);
            }
        }

        var fileList = new Array();
        $(document).ready(function () {
            // 多图上传
            $("#multipleFile").fileinput({
                uploadUrl: ctx + 'common/uploads',
                maxFileCount:'2',
                allowedFileExtensions: ['jpg', 'png', 'jpeg'],
                uploadAsync: false
            }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
                var rsp = data.response;
                log.info("return urls：" + rsp.urls)
                log.info("reutrn fileNames：" + rsp.fileNames)
                log.info("reutrn newFileNames：" + rsp.newFileNames)
                log.info("return originalFilenames：" + rsp.originalFilenames)
                if (rsp.urls && rsp.urls != 'undefined') {
                    var urlArray = rsp.urls.split(",");
                    var fileNameArray = rsp.fileNames.split(",");
                    var newFileNameArray = rsp.newFileNames.split(",");
                    var oriFileNameArray = rsp.originalFilenames.split(",");
                    for (var i = 0; i < urlArray.length; i++) {
                        fileList.push({
                            'url':urlArray[i],
                            'fileName':newFileNameArray[i],
                            'oriFileName':oriFileNameArray[i],
                            'filePath':fileNameArray[i]
                        })
                    }
                }

            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            });
        });
    </script>
</body>
</html>